from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.wtf import CSRFProtect
from flask_session import Session
import redis
from Config import Config , config_map


db = SQLAlchemy()

def create_app(config_name):

    app = Flask(__name__)

    config_class = config_map.get('config_name')

    app.config.from_object(config_class)  # 给app对象添加配置

    # db = SQLAlchemy(app)  # 给app设置数据库
    db.init_app(app)

    CSRFProtect(app)  # 开启CSRF保护

    Session(app)  # 给app设置session, 目的是持久化操作，不需次次登录， 把session存到redis里

    redis_store = redis.StrictRedis(host=Config.REDIS_HOST,port=Config.REDIS_PORT, decode_responses=True)  # 创建redis（存储验证信息）

    from iHome.index import Blue
    app.register_blueprint(Blue)  # 注册蓝图对象
    return app